package app.util.image.action
{
	
	import flash.display.*;
	import flash.filters.*;
	import flash.geom.*;
	
	public class FlawRepairAction extends PartialProcessingAction
	{	
		public override function getDescription():String
		{
			return "Flaw Repair";
		}
		
		protected override function doParticalProcess(cur:BitmapData, p:Array, str:Number):BitmapData
		{			
			var ret:BitmapData = cur.clone();
		 	var startX:int = p[0] - p[2] / Math.sqrt(2);
		 	var startY:int = p[1] - p[2] / Math.sqrt(2);
		 	var endX:int = p[0] + p[2] / Math.sqrt(2);
		 	var endY:int = p[1] + p[2] / Math.sqrt(2);
		 	
		 	var isRelevant:Function = function(left:uint, right:uint):Boolean{
		 		var lR:uint = (left & 0x00ff0000) >> 16;
				var lG:uint = (left & 0x0000ff00) >> 8;
				var lB:uint = left & 0x000000ff;
				var rR:uint = (right & 0x00ff0000) >> 16;
				var rG:uint = (right & 0x0000ff00) >> 8;
				var rB:uint = right & 0x000000ff;
				
				return !((lR - rR > 5) && (lG - rG > 5) && (lB - rB > 5));
		 	}
		 	
		 	for(var i:int = startY; i <= endY; i++){
		 		var sampled:uint = ret.getPixel(startX, i);
		 		for(var j:int = startX; j <= endX; j++){
		 			var current:uint = ret.getPixel(j, i);
		 		 	if(!isRelevant(sampled, current)){
		 		 		ret.setPixel(j, i, sampled);
		 		 	}
		 		 	else{
		 		 	sampled = ret.getPixel(j, i);
		 		 	}
		 		}
		 	}
            return ret;

		}
	}
}
